import 'package:acgn_pioneer/models/author_model.dart';
import 'package:acgn_pioneer/widgets/reader/models/_reader_state.dart';

import '../../../public.dart';

/// 漫画服务类
/// 
class ReaderComicService {
  ReaderState state;

  ReaderComicService({this.state});

  /// 漫画数据
  Future<ComicModel> getComicData(int comicId) async {
    var comicInfo = await ACGN.http.fetch<ComicModel>(
      ACGN.config.interfaceApi.getcomicdata,
      queryParameters: {'comic_id': comicId},
    );
    if (comicInfo is ComicModel == false) {
      return ComicModel(id: comicId, isNoCopyright: true);
    }

    /// 漫画标签数据
    var re = await ACGN.http.fetch<ComicModel>(
        ACGN.config.interfaceApi.getcomicranktags,
        queryParameters: {'comic_ids': comicId},
        isParse: false);
    comicInfo.tagStr = re[comicId];
    return comicInfo;
  }

  /// 绑定上下章节关系 + 章节map化
  installChapterRelation(List<ChapterModel> chapters) {
    for (var i = 0; i < chapters.length; i++) {
      var chapter = chapters[i];
      state.chapterListMap[chapter.id] = chapter;
      if (i != 0) {
        chapter.prevId = chapters[i - 1].id;
      }
      if (i != chapters.length - 1) {
        chapter.nextId = chapters[i + 1].id;
      }
    }
  }

  Future<List<AuthorModel>> getComicinfoRoleDetail(comicId) async {
    return await ACGN.http.fetch<AuthorModel>(
      ACGN.config.interfaceApi.getcomicinfoRoleDetail,
      queryParameters: {'comic_id': comicId},
    );
  }
}
